package com.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.constant.MessageConstant;
import com.youdianda.dto.YddCatePageDTO;
import com.youdianda.entity.YddCate;
import com.youdianda.result.PageResult;
import com.youdianda.result.Result;
import com.youdianda.service.YddCateService;
import com.youdianda.mapper.YddCateMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author xliu
 * @description 针对表【ydd_cate】的数据库操作Service实现
 * @createDate 2023-05-14 16:01:34
 */
@Service
public class YddCateServiceImpl extends ServiceImpl<YddCateMapper, YddCate>
        implements YddCateService {

    /**
     * 查询分类列表
     *
     * @return
     */
    public Result rowInfo(String id) {
        //非空判断
        if (id == null || id == "") {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        YddCate byId = getById(id);
        return Result.success(byId);
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result list(YddCatePageDTO dto) {
        //非空判断
        if (dto.getPage() == null) {
            dto.setPage("1");
        }
        if (dto.getLimit() == null) {
            dto.setLimit("10");
        }
        //创建IPage分页对象,设置分页参数
        IPage<YddCate> page = new Page<>(Integer.valueOf(dto.getPage()), Integer.valueOf(dto.getLimit()));
        //查询数据
        LambdaQueryWrapper<YddCate> eq = Wrappers.<YddCate>lambdaQuery().like(StringUtils.hasLength(dto.getCatename()),YddCate::getCatename, dto.getCatename());
        //开启分页
        page(page, eq);
        //封装
        PageResult pageResult = new PageResult();
        pageResult.setCount((int) page.getTotal());//总数
        pageResult.setTotalPages((int) page.getPages());//总页数
        pageResult.setPageSize((int) page.getSize());//每页数量
        pageResult.setCurrentPage((int) page.getCurrent());//当前页码
        pageResult.setData(page.getRecords());
        return Result.success(pageResult);
    }

    /**
     * 新增分类
     *
     * @param yddCate
     * @return
     */
    @Override
    @Transactional
    public Result add(YddCate yddCate) {
        //非空判断
        if (yddCate.getCatename() == null) {
            return Result.error(MessageConstant.NAME_NOT_FOUND);
        }
        //查询数据是否已存在
        LambdaQueryWrapper<YddCate> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddCate::getCatename,yddCate.getCatename());
        YddCate one = getOne(lqw);
        if (one!=null){
            return Result.error(MessageConstant.IS_FOUND);
        }
        //默认值
        yddCate.setDefault_data("1");
        save(yddCate);
        return Result.success();
    }

    /**
     * 删除分类
     *
     * @return
     */
    @Override
    @Transactional
    public Result del(String id) {
        //非空判断
        if (id == null || id == "") {
            return Result.error(MessageConstant.NOT_FOUND);
        }
        removeById(id);
        return Result.success();
    }

    /**
     * 修改数据
     *
     * @param yddCate
     * @return
     */
    @Override
    @Transactional
    public Result update(YddCate yddCate) {
        //非空判断
        if (yddCate.getCatename() == ""||yddCate.getCatename()==null) {
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        updateById(yddCate);
        return Result.success();
    }

    /**
     * 分类列表-全部
     *
     * @param
     * @return
     */
    @Override
    public Result all() {
        List<YddCate> list = list();
        return Result.success(list);
    }


}




